/**
 * 文档作者: wj
 * 创建时间：2018年05月24日
 * 修改时间：2018年05月24日
 * 描述信息：付款页面
 */
import React, {Component} from 'react';
import './payment.scss';
import util from '../../util/util';
import userModel from '../api/user.model';

class Payment extends Component {
    constructor(props) {
        super(props);
        this.payment = util.getQueryStringByName('payment');
        this.price = util.getQueryStringByName('price');
        this.sku = util.getQueryStringByName('sku')
        this.state = {
            isSelected: false
        }
        util.getApiData('post', '/svcBusiUser/svcLoadSelfJifenGroupBy', {
            uid: userModel.svcRegWithWx.uid,
            apptoken: userModel.svcRegWithWx.apptoken,
        }).then(res => {
            this.jifenSum = res.data.info.data[0].jifen_sum;
        })
    }
    render() {
        document.body.style.background = '#f6f5f6';
        return <div className='payment-component top-center'>
            <div className='header both-left-center'>
                <label>消费总额</label><input placeholder='询问服务员后输入' />
            </div>
            <p className='integral both-sides'>
                <span className='both-left-center'>
                <img alt='logo' className='logo1' src={require('../../assets/img/good.png')} />100=10元</span>
                <img alt='logo' onClick={this.onSelect.bind(this)} className='logo2' src={require(`../../assets/img/${this.state.isSelected ? 'yes' : 'radio'}.png`)} />
            </p>
            <div className='footer'>
                <p className='top'>实付金额</p>
                <p className='bottom grean'>¥ {this.payment}</p>
            </div>
            <button onClick={this.onSubmit.bind(this)} className='btn grean'>确认买单</button>
        </div>
    }
    /**
     * 点击选中
     */
    onSelect() {
        this.setState({
            isSelected: !this.state.isSelected
        })
        this.payment = !this.state.isSelected ? this.payment - (this.jifenSum / 10) : this.payment + (this.jifenSum / 10);
    }
    /** 提交 */
    onSubmit() {
        const selected = this.state.isSelected;
        util.getApiData('post', '/svcShop/svcOrderUnifyEat', {
            uid: userModel.svcRegWithWx.uid,
            apptoken: userModel.svcRegWithWx.apptoken,
            order_src: selected ? '优惠买单' : '微商城',
            fee_total: this.payment.toString(),
            fee_sku: this.price.toString(),
            fee_jifen: selected ? this.jifenSum.toString() : '0',
            sku: this.sku,
            buy_nums: util.getQueryStringByName('buy_nums'),
            fee_tran: util.getQueryStringByName('fee_tran')
        }).then(res => {
            if (res.data.res === 1) {
                util.getApiData('post', '/svcShop/svcPayWxGenOrder', {
                    uid: userModel.svcRegWithWx.uid,
                    apptoken: userModel.svcRegWithWx.apptoken,
                    openid: userModel.svcRegWithWx.uinfo.wx_openid,
                    order_id: res.data.info.data
                }).then(data => {
                    window.WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', data, (result) => {
                            console.log(result);
                        }
                    )
                })
            }
        })
    }
}

export default Payment;
